package com.example.demo;

import java.nio.charset.StandardCharsets;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import javax.annotation.PostConstruct;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Component
public class ParseHeaderFromFile {

	@Value("${baseDir}")
	String baseDir;

	public static void main(String[] args) {
//		parseHeaders().forEach((k,v)->System.out.println(k+"=="+v));
//		parseGraphQLHeaders().forEach((k,v)->System.out.println(k+"=="+v));
//		parseGitconnectedHeaders().forEach((k,v)->System.out.println(k+"=="+v));
	}

	Map<String, String> htmlHeaders;
	Map<String, String> gitconnectedHeaders;
	Map<String, String> graphQLHeaders;

	@PostConstruct
	public void init() {
		htmlHeaders = parseHeaders();
		gitconnectedHeaders = parseGitconnectedHeaders();
		graphQLHeaders = parseGraphQLHeaders();
		log.info("htmlHeaders:{}",htmlHeaders);
		log.info("gitconnectedHeaders:{}",gitconnectedHeaders);
		log.info("parseGraphQLHeaders:{}",graphQLHeaders);
	}
	
	public Map<String, String> getHtmlHeaders() {
		return htmlHeaders;
	}
	
	public Map<String, String> getGitconnectedHeaders() {
		return gitconnectedHeaders;
	}
	
	public Map<String, String> getGraphQLHeaders() {
		return graphQLHeaders;
	}

	Map<String, String> parseHeaders() {
		Map<String, String> htmlHeaders = new LinkedHashMap<>();
		List<String> lines = FileUtil.readLines(baseDir + "\\list-headers.txt", StandardCharsets.UTF_8);
		for (int i = 0; i < lines.size(); i += 2) {
			if (StrUtil.isBlank(lines.get(i))) {
				break;
			}
			htmlHeaders.put(StrUtil.sub(lines.get(i), 0, lines.get(i).length() - 1), lines.get(i + 1));
		}
		return htmlHeaders;
	}

	Map<String, String> parseGitconnectedHeaders() {
		Map<String, String> gitconnectedHeaders = new LinkedHashMap<>();
		List<String> lines = FileUtil.readLines(baseDir + "\\gitconnected-headers.txt", StandardCharsets.UTF_8);
		for (int i = 0; i < lines.size(); i += 2) {
			if (StrUtil.isBlank(lines.get(i))) {
				break;
			}
			gitconnectedHeaders.put(StrUtil.sub(lines.get(i), 0, lines.get(i).length() - 1), lines.get(i + 1));
		}
		return gitconnectedHeaders;
	}

	Map<String, String> parseGraphQLHeaders() {
		Map<String, String> headers = new LinkedHashMap<>();
		List<String> lines = FileUtil.readLines(baseDir + "\\queryClapCount-headers.txt", StandardCharsets.UTF_8);
		for (int i = 0; i < lines.size(); i += 2) {
			if (StrUtil.isBlank(lines.get(i))) {
				break;
			}
			headers.put(StrUtil.sub(lines.get(i), 0, lines.get(i).length() - 1), lines.get(i + 1));
		}
		return headers;
	}
}
